import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'home',
    meta: { permission: ['admin', 'user'] } ,// 权限为 admin 或 user
    component: HomeView
  },
  {
    path: '/about',
    name: 'about',
    meta:{permission: ['admin', 'user']},
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  },
  {
    path: '/admin',
    name: 'admin',
    component: ()=>import('../views/Admin.vue'),
    meta: { permission: ['admin'] } // 权限为 admin
  },
  {
    path: '/user',
    name: 'user',
    component: () => import('../views/User.vue'),
    meta: { permission: ['user'] } // 权限为 user
  },
  {
    path: '/articleSystem',
    name: 'articleSystem',
    component: () => import('../views/articleSystem.vue'),
    meta: { permission: ['admin','user'] } 
  },
  {
    path: '/404',
    name: '404',
    meta: { permission: ['admin', 'user'] } ,// 权限为 admin 或 user
    component: ()=>import('../views/404.vue')
  },
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  console.log(to,'to')
  const permission =  'user' // 假设当前用户权限为 admin 或 user
  if (to.meta && to.meta.permission && !to.meta.permission.includes(permission)) {
    // 如果当前用户没有访问该路由的权限，则跳转到 404 页面
    next({ name: '404' })
  } else {
    next()
  }
})

export default router
